<template>
      <div class="stats-content">
        <div 
          v-for="(item, index) in statsData" 
          :key="index" 
          class="stat-item"
        >
          <div class="progress-ring">
            <svg class="progress-circle" viewBox="0 0 100 100">
              <circle class="progress-circle-bg" cx="50" cy="50" r="45" />
              <circle 
                class="progress-circle-fill" 
                :class="`${item.type}-fill`"
                cx="50" cy="50" r="45"
                :style="{
                  strokeDashoffset: circumference * (1 - animatedPercent[index] / 100),
                  opacity: animatedPercent[index] > 0 ? 1 : 0
                }"
              />
            </svg>
            <div 
              class="progress-text" 
              :style="{ color: item.color }"
            >
              {{ Math.round(animatedPercent[index]) }}%
            </div>
          </div>
          <div class="stat-label">{{ item.label }}</div>
        </div>
      </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue'
  
  const props = defineProps({
    title: {
      type: String,
      default: '工单统计'
    },
    statsData: {
      type: Array,
      default: () => [
        {
          value: 25,
          label: '今日上报',
          percent: 54,
          type: 'completion',
          color: '#FFA500'
        },
        {
          value: 16,
          label: '今日完成',
          percent: 32,
          type: 'delay',
          color: '#4facfe'
        }
      ]
    },
    duration: {
      type: Number,
      default: 1.5
    }
  })
  
  const circumference = 2 * Math.PI * 45
  const animatedPercent = ref(props.statsData.map(() => 0))
  
  onMounted(() => {
    // 为每个进度条启动动画
    props.statsData.forEach((item, index) => {
      animateProgress(index, item.percent)
    })
  })
  
  const animateProgress = (index, targetPercent) => {
    const startTime = Date.now()
    const duration = props.duration * 1000
    
    const animate = () => {
      const elapsed = Date.now() - startTime
      const progress = Math.min(elapsed / duration, 1)
      
      // 使用缓动函数使动画更自然
      const easedProgress = easeOutQuad(progress)
      animatedPercent.value[index] = easedProgress * targetPercent
      
      if (progress < 1) {
        requestAnimationFrame(animate)
      }
    }
    
    requestAnimationFrame(animate)
  }
  
  // 缓动函数 - 二次缓出
  const easeOutQuad = (t) => {
    return t * (2 - t)
  }
  </script>
  
  <style scoped>
  .stats-container {
    background: linear-gradient(to bottom, #0052a3, #003366);
    width: 600px;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
    color: white;
  }
  
  .stats-title {
    text-align: center;
    font-size: 24px;
    margin-bottom: 30px;
    font-weight: bold;
    position: relative;
    animation: fadeIn 0.8s ease-out;
  }
  
  .stats-title::after {
    content: "";
    display: block;
    width: 100px;
    height: 3px;
    background: linear-gradient(to right, #00a8ff, #4facfe);
    margin: 10px auto 0;
    animation: lineExpand 0.8s ease-out;
  }
  
  .stats-content {
    width: 100%;
    height: calc(100% - 60px);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
  }
  
  .stat-item {
    width: 50%;
    height: 50%;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    animation: fadeInUp 0.8s ease-out;
  }
  
  .stat-value {
    font-size: 26px;
    font-weight: bold;
    animation: countUp 1s ease-out;
  }
  
  .stat-label {
    font-size: 14px;
    margin-top: 10px;
    color: #fff;
  }
  
  /* 环形图样式 */
  .progress-ring {
    position: relative;
    width: 100px;
    height: 100px;
    margin: 0 auto;
    margin-top: 20px;
  }
  
  .progress-circle {
    width: 100%;
    height: 100%;
    transform: rotate(-90deg);
  }
  
  .progress-circle-bg {
    fill: none;
    stroke: rgba(255, 255, 255, 0.1);
    stroke-width: 8;
  }
  
  .progress-circle-fill {
    fill: none;
    stroke-width: 8;
    stroke-linecap: round;
    stroke-dasharray: 283; /* 2πr (r=45) */
    transition: opacity 0.3s ease;
  }
  
  .completion-fill {
    stroke: #FFA500; /* 橙色 */
  }
  
  .delay-fill {
    stroke: #4facfe; /* 浅蓝色 */
  }
  
  .progress-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 18px;
    font-weight: bold;
    transition: all 0.3s ease;
  }
  
  /* 动画定义 */
  @keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
  }
  
  @keyframes lineExpand {
    from { width: 0; opacity: 0; }
    to { width: 100px; opacity: 1; }
  }
  
  @keyframes countUp {
    from { 
      opacity: 0;
      transform: translateY(20px);
      text-shadow: 0 0 10px rgba(255,255,255,0.5);
    }
    to { 
      opacity: 1;
      transform: translateY(0);
      text-shadow: none;
    }
  }
  
  @keyframes fadeInUp {
    from {
      opacity: 0;
      transform: translateY(30px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
  </style>